package qc;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

public class ChangePasswordBCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		HashMap params = (HashMap) arg0.get(arg0.size()-1);
		String username = (String) params.get("userToChange");
		String newPass = (String) params.get("newPassword");
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement update = null;
		
		try {
			update = con.prepareStatement("UPDATE User SET UserPass = ? WHERE UserName = ?");
			update.setString(1, newPass);
			update.setString(2, username);
			update.execute();
		} catch (SQLException e) {
			MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
			params.put("serverMessage", "Password was not changed");
			return params;
		}
		
		params.put("serverMessage", "Password was changed for user: "+ username);
		MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
		return params;
	}

}
